package com.leesche.zcsp.node.app.v1.controller.operate;

import com.leesche.zcsp.node.app.v1.controller.admin.AppAdminController;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevParam;
import com.leesche.zcsp.node.web.service.PubDevParamService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDevParamDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDevParamCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by kaider on 2016/11/18.
 * 设备参数
 */
@Controller
@RequestMapping("/app/v1/operate/devParam")
public class AppDevParamController extends AppAdminController {
    private final static Logger LOGGER = LoggerFactory.getLogger(AppDevParamController.class);

    @Autowired
    private PubDevParamService devParamService;

    /**
     * 设备参数设置
     * @param deviceId 设备id
     * @param devParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDevParam", method = RequestMethod.POST)
    public Map<String,Object> addDevParam(
            Long deviceId,
            PubDevParam devParam
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            devParam.setAdminid(adminId);
            devParam.setDeviceid(deviceId);
            devParamService.insert(devParam);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备参数状态
     * @param paramCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDevParamView", method = RequestMethod.POST)
    public Map<String,Object> editDevParamView(
            QueryPubDevParamCondition paramCondition
    ){
        try {
            PubDevParamDto paramDto = devParamService.findByCondition(paramCondition);
            return WebResult.success().put("data",paramDto).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备参数
     * @param devParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDevParam", method = RequestMethod.POST)
    public Map<String,Object> editDevParam(
            PubDevParam devParam
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            devParam.setAdminid(adminId);
            devParamService.updateByPrimaryKeySelective(devParam);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

}
